﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_DSJCJ.Common;
using System.Data;
using Webdiyer.WebControls.Mvc;

namespace ElingOA6_DSJCJ.Controllers
{
    /// <summary>
    /// 通讯录
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AddrBookController : Controller
    {
        //
        // GET: /AddrBook/
        private BLL.Common commonBLL = new BLL.Common();
        BLL.HRDept hrDeptBLL = new BLL.HRDept();

        public ActionResult Index(int id = 1, string deptNo = "", string EmplName="")
        {
             //bool isLogin = string.IsNullOrEmpty(OperateSession.get(GlobalConstant.PARAM_LOGINID)) ? false : true;

             // if (!isLogin)
             // {
             //     Response.Write("<script>alert('需要登录后才能查看通讯录');window.location.href='"+Url.Action("Index","Index")+"';</script>");
             //     Response.Flush();
             //     Response.End();
             //     return null;
             // }
            string entityNo = GlobalConstant.ENTITYNO;
            int pageSize = 15;
            int pageIndex = id;
            int total = 0;
            string fieldOrder = " FDeptSeq,FSeq,FDutyNo ";
            string strWhere = " (FEmpStatus='01' OR FEmpStatus='02') ";//正式和试用
            deptNo = Fetch.estimateString(deptNo, CheckGetEnum.Safety);
            if (deptNo != "")
            {
                //strWhere += " FDeptNo='" + deptNo + "'";
                strWhere += " AND FDeptNo IN (SELECT  FDeptNo FROM dbo.fn_GetSubHRDept('" + deptNo + "','" + entityNo + "') ) AND FIsMainPosition='Y' ";
                if (!string.IsNullOrEmpty(EmplName))
                {
                    strWhere += "AND FEmpName Like  '%" + EmplName + "%'";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(EmplName))
                {
                    strWhere += "AND FEmpName Like  '%" + EmplName + "%'";
                }
            }
            string fiedls = "*";
            string tableName = "v_HREmpl";
            DataSet ds = commonBLL.GetListByPage(tableName, fiedls, pageSize, pageIndex, strWhere, fieldOrder, 0, out total);
            List<Models.HREmplGrid> tmpHREmplList = ViewDataRowToModel.DataTableToListHREmpl(ds.Tables[0]);

            PagedList<Models.HREmplGrid> hrEmplLIst = new PagedList<Models.HREmplGrid>(tmpHREmplList, id, pageSize, total);


            List<Model.HRDept> deptList = hrDeptBLL.GetAllDeptNoAndName(entityNo);
            List<SelectListItem> itemList = new List<SelectListItem>();
            itemList.Add(new SelectListItem() { Text = "请选择须查询的部门", Value = "" });
            foreach (Model.HRDept model in deptList.Where(c => c.FParentDeptNo == null).ToList())
            {
                itemList.Add(new SelectListItem() { Text = model.FDeptName, Value = model.FDeptNo });
                SetSubDept(itemList, deptList, model.FDeptNo, "┗");
            }

            ViewData["deptItemList"] = itemList;
            ViewData["deptNo"] = deptNo;
            return View(hrEmplLIst);
        }
        /// <summary>
        /// 递归绑定下级部门
        /// </summary>
        /// <param name="itemList"></param>
        /// <param name="deptList"></param>
        /// <param name="preDeptNo"></param>
        /// <param name="spaceStr"></param>
        private void SetSubDept(List<SelectListItem> itemList, List<Model.HRDept> deptList, string preDeptNo, string spaceStr)
        {
            string subSpace = System.Web.HttpUtility.HtmlDecode("&nbsp;") + spaceStr;
            foreach (Model.HRDept model in deptList.Where(c => c.FParentDeptNo == preDeptNo).ToList())
            {
                itemList.Add(new SelectListItem() { Text = subSpace + model.FDeptName, Value = model.FDeptNo });
                SetSubDept(itemList, deptList, model.FDeptNo, subSpace);
            }
        }
    }
}
